﻿Imports MySql.Data.MySqlClient

Public Class add1_1
    Public key As String
    Public SQL As MySqlConnection
    Public selectedEmployee As String


    Public Sub New(ByRef SQLConnection As MySqlConnection)
        InitializeComponent()
        SQL = SQLConnection
        Dim bFullScreen As Boolean



    End Sub
    

    Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        showdata()
        '  SELECT * from Employee where (dept_id in SELECT * from Department where dept_name like "'%"+textfield+"%'") or emp_name like * "'%"+textfield+"%'" or emp_surname like "'%"+textfield+"%'" or emp_position like "'%"+textfield+"%'" or emp_level like "'%"+textfield+"%'"
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If (selectedEmployee = "") Then
            MsgBox("Please select your data", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Warning Message")
        Else
            Dim nextForm As add1_2 = New add1_2(SQL, selectedEmployee)
            nextForm.Show()
            Me.Close()
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim mainForm As main = New main()
        mainForm.Show()
        Me.Close()
    End Sub

    Private Sub add1_1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        selectedEmployee = ""
        showdata()
    End Sub

    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown

        If e.KeyCode = "13" Then
            showdata()
        End If

    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub

    Private Sub ListView1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseClick
        selectedEmployee = ListView1.SelectedItems(0).Text
    End Sub

    Private Sub ListView1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDoubleClick
        If (selectedEmployee = "") Then
            MsgBox("Please select your data", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Warning Message")
        Else
            Dim nextForm As add1_2 = New add1_2(SQL, selectedEmployee)
            nextForm.Show()
            Me.Close()
        End If
    End Sub

    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

    End Sub

    Private Sub showdata()
        Dim mySqlCommand As New MySqlCommand
        Dim mySqlAdaptor As New MySqlDataAdapter
        Dim mySqlReader As MySqlDataReader

        key = TextBox1.Text



        If SQL.State = ConnectionState.Closed Then
            SQL.Open()
        End If


        'mySqlCommand.CommandText = "SELECT DISTINCT * from Employee where dept_id in (SELECT dept_id from Department where dept_name like" + " '%" + TextBox1.Text + "%' )" + " or emp_name like " + "'%" + TextBox1.Text + "%'" + " or emp_surname like " + "'%" + TextBox1.Text + "%'" + " or emp_position like " + "'%" + TextBox1.Text + "%'" + " or emp_level like " + "'%" + TextBox1.Text + "%'" + ";"
        mySqlCommand.CommandText = "SELECT * FROM Employee JOIN Department where ( Employee.dept_id = Department.dept_id ) and ( Department.dept_name like" + "'%" + TextBox1.Text + "%' or Employee.emp_name like " + "'%" + TextBox1.Text + "%' or Employee.emp_surname like " + "'%" + TextBox1.Text + "%' or Employee.emp_level like " + "'%" + TextBox1.Text + "%'" + "or Employee.emp_code like " + "'%" + TextBox1.Text + "%'" + "or Employee.emp_position like " + "'%" + TextBox1.Text + "%'" + "or Employee.emp_gender like " + "'%" + TextBox1.Text + "%') and emp_status = '1' order by Employee.emp_name;"
        ' mySqlCommand.CommandText = 
        mySqlCommand.Connection = SQL
        mySqlAdaptor.SelectCommand = mySqlCommand
        Try
            mySqlReader = mySqlCommand.ExecuteReader
        
            ListView1.Items.Clear()

            While (mySqlReader.Read())


                With ListView1.Items.Add(mySqlReader("emp_code"))
                    Try
                        .subItems.add(mySqlReader("emp_gender"))
                    Catch
                        .subItems.add("-")
                    End Try
                    .subItems.add(mySqlReader("emp_name") + " " + mySqlReader("emp_surname"))
                    .subItems.add(mySqlReader("emp_position"))
                    .subItems.add(mySqlReader("dev_name"))
                    .subItems.add(mySqlReader("dept_name"))
                    .subItems.add(mySqlReader("emp_level"))


                End With


            End While
        Catch
        End Try
        SQL.Close()
    End Sub

    Private Sub ListView1_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged

    End Sub
End Class